package com.arvinclub.web.filter;

import com.arvinclub.model.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 未登录拦截
 */
@WebFilter(filterName = "adminFilter",urlPatterns = "/admin/*")
public class AdminFilter implements Filter
{
    public void destroy(){}

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException
    {
        User user = (User) ((HttpServletRequest) req).getSession().getAttribute("user");
        if (user != null && user.getAdmin() > 0)
            chain.doFilter(req, resp);
        else
            ((HttpServletResponse)resp).sendRedirect("/user/blogs.html");
    }

    public void init(FilterConfig config){ }

}
